﻿package com.victim.snake.graphics.mainClass {
	import flash.display.Graphics;
	import flash.display.Sprite;
	import com.victim.snake.map.MapEvent;
	import com.victim.snake.map.Map;
	import flash.display.Bitmap;
	import com.victim.snake.util.GraphicsToll;
	import flash.display.CapsStyle;
    import flash.display.JointStyle;
	import flash.display.LineScaleMode;
	import fl.controls.Label;
	import fl.managers.StyleManager;
	import com.victim.snake.style.TextStyle;
	import flash.sensors.Accelerometer;
	import flash.events.MouseEvent;
	import com.victim.snake.user.ProcessMsg;
	import com.victim.snake.room.Room;
	
	
	//房间列表项类	
	public class RoomGridItem extends Sprite{
		//缩略图位置
		private var roomId:Label=new Label();
		private var roomTitle:Label=new Label();
		private var playersNum:Label=new Label();
		private var bitMap:Bitmap=null;
		public var transparenDiv:TransparenDiv=new TransparenDiv();
		private var roomGrid:RoomGrid=null;
		
		//背景/鼠标经过时/按下时
		public var roomGridItemBackground:Bitmap=null;
		public var roomGridItemOnMouseOverBackground:Bitmap=null;	
		public var roomGridItemOnSelectBackgroundDown:Bitmap=null;	
		public var roomGridItemSelectOnMouseOverBackground:Bitmap=null;	
		public var status:int=0;	//状态   0，末选中，1选中，2鼠标移动到其上
		
		//房间对象
		private var room:Room=null;
		
		//构造函数
		public function RoomGridItem(x:int,y:int,rg:RoomGrid):void{
			this.roomGrid=rg;
			this.x=x;
			this.y=y;

			
			transparenDiv.doubleClickEnabled=true;	//开启双击事件
			
			//添加单击进入房间事件
			//this.addEventListener(MouseEvent.CLICK,onMouseEvent);
		}
		
		//添加房间数据
		public function addRoom(room:Room){
			this.room=room;
			//加载地图
			this.room.getMap().addEventListener(MapEvent.IsLoader,mapLoaderEvent);
			this.room.getMap().load();

		}
		
		//房间列表地图加载完成
		function mapLoaderEvent(e:MapEvent):void{
			//trace("显示房间信息");
			
			
			
			
			//显示缩略图
			var m:Map = Map(e.currentTarget);
			m.removeEventListener(MapEvent.IsLoader,mapLoaderEvent);	//删除当前监听			
			this.bitMap=new Bitmap(m.getPreviewImage().bitmapData);
			this.bitMap.name="map";
			this.bitMap.width=65;
			this.bitMap.height=51;			
			this.bitMap.x=8;
			this.bitMap.y=7;
			this.addChildAt(this.bitMap,0);	
			
			
			//显示房间ID
			roomId.text=this.room.getId()+"";
			roomId.name = "roomId";
			roomId.x = 83;
			roomId.y = 10;
			roomId.width=40;
			StyleManager.setComponentStyle(roomId,"textFormat",TextStyle.getRoomIdStyle());
			this.addChild(roomId);
			
			//显示房间名
			roomTitle.width=130;
			roomTitle.text=this.room.getTitle();
			roomTitle.name="roomTitle";
			roomTitle.x = 142;
			roomTitle.y = 10;			
			StyleManager.setComponentStyle(roomTitle,"textFormat",TextStyle.getRoomTitleStyle());
			this.addChild(roomTitle);
			
			//玩家人数
			playersNum.text=this.room.getPlayers().length+"";
			playersNum.name="playersNum";
			playersNum.x = 210;
			playersNum.y = 36;
			playersNum.width=28;
			StyleManager.setComponentStyle(playersNum,"textFormat",TextStyle.getRoomIdStyle());
			this.addChild(playersNum);

			//透明层事件			
			this.addChild(transparenDiv);
			transparenDiv.addEventListener(MouseEvent.CLICK,onMouseEvent);
			transparenDiv.addEventListener(MouseEvent.MOUSE_OVER,onMouseOver);
			transparenDiv.addEventListener(MouseEvent.MOUSE_OUT,onMouseOut);
			transparenDiv.addEventListener(MouseEvent.DOUBLE_CLICK,onMouseDoubleClick);
		}
		
		//初使化列表项
		public function init():void{
			this.room=null;
			this.roomId.text="";
			this.roomTitle.text="";
			this.playersNum.text="";
			if(this.numChildren>0){
				this.removeChildAt(0);
			}
			
			if(this.roomGridItemBackground!=null){
				this.addChildAt(this.roomGridItemBackground,0);
			}
			
			try{
				this.removeChild(bitMap);
			}catch(e:Error){
				//trace("无房间地图");
			}
			
			this.bitMap=null;
			transparenDiv.removeEventListener(MouseEvent.CLICK,onMouseEvent);
			transparenDiv.removeEventListener(MouseEvent.MOUSE_OVER,onMouseOver);
			transparenDiv.removeEventListener(MouseEvent.MOUSE_OUT,onMouseOut);
			transparenDiv.removeEventListener(MouseEvent.DOUBLE_CLICK,onMouseDoubleClick);
			this.status=0;
		}
		
		//选择状态初使化
		public function selectInit():void{			
			this.status=0;
			this.initBackGround();
			try{
				this.addChildAt(this.roomGridItemBackground,0);			
			}catch(e:Error){
				//trace("背景末下载")
			}
			
			try{
				this.addChildAt(this.bitMap,0);	//将地图添加到列表项显示列表最底层
			}catch(e:Error){
				//trace(this.bitMap)
			}
		}
		
		private function onMouseEvent(e:MouseEvent):void{			
			this.roomGrid.selectInit();
			this.status=1;
			this.initBackGround();
			this.addChildAt(this.roomGridItemSelectOnMouseOverBackground,0);
			this.addChildAt(this.bitMap,0);	//将地图添加到列表项显示列表最底层
			
		}
		
		private function onMouseOver(e:MouseEvent):void{
			this.initBackGround();
			if(this.status==0){
				this.addChildAt(this.roomGridItemOnMouseOverBackground,0);
			}else if(this.status==1){
				this.addChildAt(this.roomGridItemSelectOnMouseOverBackground,0);
			}			
			this.addChildAt(this.bitMap,0);	//将地图添加到列表项显示列表最底层
		}
		
		private function onMouseOut(e:MouseEvent):void{
			//trace(this.status);
			this.initBackGround();
			if(this.status==0){
				this.addChildAt(this.roomGridItemBackground,0);
			}else if(this.status==1){
				this.addChildAt(this.roomGridItemOnSelectBackgroundDown,0);
			}
			this.addChildAt(this.bitMap,0);	//将地图添加到列表项显示列表最底层
		}
		
		//双击加入房间
		private function onMouseDoubleClick(e:MouseEvent):void{
			ProcessMsg.socketSend(ProcessMsg.generateUTF8Msg(6,this.room.getId()));

		}
		
		//清空背景
		public function initBackGround():void{
			try{
				this.removeChild(this.roomGridItemBackground);				
			}catch(e:Error){
				//trace("列表项无添加此背景");
			}
			
			try{
				this.removeChild(this.roomGridItemOnMouseOverBackground);				
			}catch(e:Error){
				//trace("列表项无添加此背景");
			}
			
			try{
				this.removeChild(this.roomGridItemOnSelectBackgroundDown);
			}catch(e:Error){
				//trace("列表项无添加此背景");
			}
			
			try{
				this.removeChild(this.roomGridItemSelectOnMouseOverBackground);
			}catch(e:Error){
				//trace("列表项无添加此背景");
			}
		}

	}
	
}
